package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.oa.entity.*;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.mapper.IVeOaCarSetMapper;
import org.edu.modules.oa.mapper.IVeOaNewsMapper;
import org.edu.modules.oa.service.IVeOaNewsService;
import org.edu.modules.oa.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class VeOaNewsServiceImpl extends ServiceImpl<IVeOaNewsMapper, VeOaNewsEntity>   implements IVeOaNewsService {
    @Resource
    private IVeOaNewsMapper iVeOaNewsMapper;
    @Resource
    private IVeFileFilesMapper veFileFilesMapper;
    @Override
    public int addNews(VeOaNewsEntity veOaNewsEntity, VeFileFilesEntity veFileFilesEntity) {
        int res = 0;
        if (veFileFilesEntity == null) {
            res = iVeOaNewsMapper.insert(veOaNewsEntity);
        }
        if (veFileFilesEntity != null) {
            int add = veFileFilesMapper.insert(veFileFilesEntity);
            if (add != 0) {
                veOaNewsEntity.setFileIds(veFileFilesEntity.getId());

                Integer reust = iVeOaNewsMapper.insert(veOaNewsEntity);
            }
        }
        return res;
    }

    //显示表格
    public HashMap<String, Object> showTable(Integer startPage, Integer pageSize, VeOaNewsEntity veOaNewsEntity) {

        //动态sql模糊查询收阅人
        List<String> names = new ArrayList<>();
        if(!"".equals(veOaNewsEntity.getReaders())) {
            String[] arr = veOaNewsEntity.getReaders().split(",");
            System.out.println(arr[0]);
            for (String s : arr) {
                if(!"".equals(s))
                    names.add(s);
                System.out.println("你模糊查询了几个收阅人"+s);
            }
        }

        List<VeOaNewsVo> voelist = iVeOaNewsMapper.showTable((startPage - 1) * pageSize, pageSize,veOaNewsEntity,names);
        for (VeOaNewsVo VeOaNewsVo : voelist) {
            if("".equals(VeOaNewsVo.getName())||VeOaNewsVo.getName()==null){
                VeOaNewsVo.setName("没有附件");
            }
            if(VeOaNewsVo.getReaders()==null||"".equals(VeOaNewsVo.getReaders())){
                VeOaNewsVo.setReaders("所有用户");
            }
        }
        int count = iVeOaNewsMapper.tableCount(veOaNewsEntity,names);
        // 把查出来的东西存到map里面去
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", voelist);
        map.put("count", count);
        // 查询表格总行数
        return map;
    }

    @Override
    public List<VeOaNoticeExportVo> xporte() {
        return null;

    }

    @Override
    public int deleteRecord(Long id) {

        VeOaNewsEntity v= new VeOaNewsEntity();
        v.setId(id);
        v.setIsDeleted(1);
        int r2 = iVeOaNewsMapper.updateById(v);

        return r2;

    }

    @Override
    public List<VeOaNewsExportVo> export(VeOaNewsEntity veOaNewsEntity) {
        //创建查询的条件
        QueryWrapper<VeOaNewsEntity>wrapper = new QueryWrapper<>();
        if(veOaNewsEntity.getId()!=null&&veOaNewsEntity.getId()!=0){
            wrapper.eq("id",veOaNewsEntity.getId());

        }
        wrapper.eq("is_deleted",0);

        //查数据库，获取所有数据的集合
        List<VeOaNewsEntity>list = iVeOaNewsMapper.selectList(wrapper);
        //集合a是导出Excle的集合用的
        List<VeOaNewsExportVo>a = new ArrayList<>();
        for(VeOaNewsEntity v : list){
            VeOaNewsExportVo veOaNewsExportVo = new VeOaNewsExportVo();
            veOaNewsExportVo.setTitleStr(v.getTitleStr());
            veOaNewsExportVo.setContent(v.getContent());
            veOaNewsExportVo.setReaders(v.getReaders());
            if("".equals(v.getReaders())||v.getReaders()==null){
                veOaNewsExportVo.setReaders("所有用户");

            }
//            veOaNewsExportVo.setCreateTime(v.getCreateTime());
            veOaNewsExportVo.setCreateBy(v.getCreateBy());
            //时间类型转换成字符串
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            veOaNewsExportVo.setCreateTime(formatter.format(v.getCreateTime()));

            a.add(veOaNewsExportVo);
        }
        return a;
    }

    @Override
    public int amendNews(VeOaNewsEntity veOaNewsEntity, VeFileFilesEntity veFileFilesEntity) throws Exception {
        QueryWrapper<VeOaNewsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", veOaNewsEntity.getId());
        int reust = 0;
        if (veFileFilesEntity == null) {
            reust = iVeOaNewsMapper.update(veOaNewsEntity, wrapper);
        }
        if (veFileFilesEntity != null) {
            System.out.println("有文件");
            int add = veFileFilesMapper.insert(veFileFilesEntity);

            if (add != 0) {
                veOaNewsEntity.setFileIds(veFileFilesEntity.getId());

                reust = iVeOaNewsMapper.update(veOaNewsEntity, wrapper);
                System.out.println("文件id改了啊"+reust);
            }
        }
        return reust;
    }
}
